using System;
using EPiServer.Common;
using EPiServer.Common.Publishing;
using EPiServer.Common.Security;
using EPiServer.Community.ImageGallery;
using EPiServer.Community.MyPage;
using EPiServer.Community.MyPage.Queries;
using EPiServer.Community.Poll;

namespace EPiAbstractions.Community.MyPage
{
    public class MyPageHandlerFacade : IMyPageHandlerFacade
    {
        private static MyPageHandlerFacade _instance;

        public MyPageHandlerFacade()
        {
            MyPageHandler.Register += OnRegister;
            MyPageHandler.UserRemoved += OnUserRemoved;
            MyPageHandler.Visit += OnVisit;
            MyPageHandler.BlockAdded += OnBlockAdded;
            MyPageHandler.BlockRemoved += OnBlockRemoved;
        }

        public static MyPageHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new MyPageHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IMyPageHandlerFacade Members

        public event EPiServerCommonEventHandler Register;
        public event EPiServerCommonEventHandler UserRemoved;
        public event EPiServerCommonEventHandler Visit;
        public event EPiServerCommonEventHandler BlockAdded;
        public event EPiServerCommonEventHandler BlockRemoved;

        public virtual Boolean IsBlocked(EPiServer.Community.MyPage.MyPage mp, IUser blockee)
        {
            return MyPageHandler.IsBlocked(mp, blockee);
        }

        public virtual UserCollection GetBlockedUsers(EPiServer.Community.MyPage.MyPage mp, Int32 page, Int32 pageSize)
        {
            return MyPageHandler.GetBlockedUsers(mp, page, pageSize);
        }

        public virtual UserCollection GetBlockedUsers(EPiServer.Community.MyPage.MyPage mp, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return MyPageHandler.GetBlockedUsers(mp, page, pageSize, out totalItems);
        }

        public virtual void AddBlock(EPiServer.Community.MyPage.MyPage mp, IUser blockee)
        {
            MyPageHandler.AddBlock(mp, blockee);
        }

        public virtual void RemoveBlock(EPiServer.Community.MyPage.MyPage mp, IUser blockee)
        {
            MyPageHandler.RemoveBlock(mp, blockee);
        }

        public virtual void UpdateMyPage(EPiServer.Community.MyPage.MyPage mp)
        {
            MyPageHandler.UpdateMyPage(mp);
        }

        public virtual void AddVisit(EPiServer.Community.MyPage.MyPage mp, IUser visitor)
        {
            MyPageHandler.AddVisit(mp, visitor);
        }

        public virtual VisitCollection GetLatestVisitors(EPiServer.Community.MyPage.MyPage mp, Int32 maxVisitors)
        {
            return MyPageHandler.GetLatestVisitors(mp, maxVisitors);
        }

        public virtual EPiServer.Community.MyPage.MyPage GetMyPage(IUser user)
        {
            return MyPageHandler.GetMyPage(user);
        }

        public virtual EPiServer.Community.MyPage.MyPage GetMyPage(IUser user, ISite site)
        {
            return MyPageHandler.GetMyPage(user, site);
        }

        public virtual EPiServer.Community.MyPage.MyPage GetMyPage(IUser user, ISite site, Boolean createMyPage)
        {
            return MyPageHandler.GetMyPage(user, site, createMyPage);
        }

        public virtual EPiServer.Community.MyPage.MyPage GetMyPage(Int32 id)
        {
            return MyPageHandler.GetMyPage(id);
        }

        public virtual PollCollection GetMyPagePolls(EPiServer.Community.MyPage.MyPage mp, Int32 page, Int32 pageSize, PollSortOrder[] order)
        {
            return MyPageHandler.GetMyPagePolls(mp, page, pageSize, order);
        }

        public virtual PollCollection GetMyPagePolls(EPiServer.Community.MyPage.MyPage mp, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                     PollSortOrder[] order)
        {
            return MyPageHandler.GetMyPagePolls(mp, page, pageSize, out totalItems, order);
        }

        public virtual Poll AddMyPagePoll(EPiServer.Community.MyPage.MyPage mp, Poll poll)
        {
            return MyPageHandler.AddMyPagePoll(mp, poll);
        }

        public virtual void RemoveMyPagePoll(EPiServer.Community.MyPage.MyPage mp, Poll poll)
        {
            MyPageHandler.RemoveMyPagePoll(mp, poll);
        }

        public virtual ImageCollection GetPortraitImages(Int32 page, Int32 pageSize, PublishState publishState, ImageSortOrder[] sortOrder)
        {
            return MyPageHandler.GetPortraitImages(page, pageSize, publishState, sortOrder);
        }

        public virtual ImageCollection GetPortraitImages(Int32 page, Int32 pageSize, PublishState publishState, out Int32 totalImages,
                                                         ImageSortOrder[] sortOrder)
        {
            return MyPageHandler.GetPortraitImages(page, pageSize, publishState, out totalImages, sortOrder);
        }

        public virtual MyPageCollection GetQueryResult(MyPageQuery query)
        {
            return MyPageHandler.GetQueryResult(query);
        }

        public virtual MyPageCollection GetQueryResult(MyPageQuery query, Int32 page, Int32 pageSize)
        {
            return MyPageHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual MyPageCollection GetQueryResult(MyPageQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return MyPageHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual VisitCollection GetQueryResult(VisitQuery query)
        {
            return MyPageHandler.GetQueryResult(query);
        }

        public virtual VisitCollection GetQueryResult(VisitQuery query, Int32 page, Int32 pageSize)
        {
            return MyPageHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual VisitCollection GetQueryResult(VisitQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return MyPageHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnRegister(String sender, EPiServerCommonEventArgs e)
        {
            if (Register != null)
                Register(sender, e);
        }

        public virtual void OnUserRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (UserRemoved != null)
                UserRemoved(sender, e);
        }

        public virtual void OnVisit(String sender, EPiServerCommonEventArgs e)
        {
            if (Visit != null)
                Visit(sender, e);
        }

        public virtual void OnBlockAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (BlockAdded != null)
                BlockAdded(sender, e);
        }

        public virtual void OnBlockRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (BlockRemoved != null)
                BlockRemoved(sender, e);
        }
    }
}